package com.carqueue.config;

import com.carqueue.interceptor.LoginInterceptor;
import com.carqueue.pojo.LimitToAuthority;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class LoginConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/toIndexPage").setViewName("/index");
        registry.addViewController("/").setViewName("/index");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());
        // intercept all request
        registration.addPathPatterns("/**");
        registration.excludePathPatterns(
                "/login",
                "/overAuthority",
                "/register",
                "/**/*.html",
                "/**/*.js",
                "/**/*.css"
        );

        /*
            set limitAuthoritySets
            higher limit user have lower limit user authority.
         */
        LimitToAuthority.limit0Authority = List.of(new String[]{"/location","/create","/join","/quit","/show","/register","/findpassword","/changepassword"});
        LimitToAuthority.limit1Authority = List.of(new String[]{"/allocation", "/showfleet"});
    }
}
